<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This class is representing a XML feed similar to this:
 * 
 * <activity:subject>
 *  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
 *  <id>http://lobstermonster.org/minime</id>
 *  <title>Tuomas Koski</title>
 *  <link rel="alternate" type="text/html" href="http://identi.ca/tkoski"/>
 * </activity:subject>
 * 
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Activitystream_Subject implements Atom_ChildElement {
    
    /**
     * @var Array of Atom_ChildElement
     */
    private $childElements = array();
    
    const ELEMENT_SUBJECT = "subject";
    
    public function addChildElement(Atom_ChildElement $childElement) {
        $this->childElements[] = $childElement;
    }
    
    /**
     * @return Atom_Feed_Link_Avatar
     */
    public function getAvatar() {
        
        foreach ($this->childElements as $elm) {
            if($elm instanceof Atom_Feed_Link_Avatar) {
                return $elm;
            }
        }
        return null;
    }
    
    /**
     * @return Atom_Feed_Title
     */
    public function getTitle() {
        
        foreach ($this->childElements as $elm) {
            if($elm instanceof Atom_Feed_Title) {
                return $elm;
            }
        }
        return null;
    }
    
    /**
     * @return Activitystream_ObjectType
     */
    public function getObjectType() {
        
        foreach ($this->childElements as $elm) {
            if($elm instanceof Activitystream_ObjectType) {
                return $elm;
            }
        }
        return null;
    }
    
    /**
     * This method should produce something like this:
     * 
     * <activity:subject>
     *  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
     *  <id>http://lobstermonster.org/minime</id>
     *  <title>Tuomas Koski</title>
     *  <link rel="alternate" type="text/html" href="http://identi.ca/tkoski"/>
     * </activity:subject>
     * 
     * @param DOMDocument$dom
     */
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(ActivityStream::AS_NS_PREFIX . ':' . self::ELEMENT_SUBJECT);
        foreach ($this->childElements as $childElement) {
            $element->appendChild($childElement->asDomElement($dom));
        }
        return $element;
    }
    
    public static function buildFromDOMElement(DOMElement $elm) {
        $as_subject = new Activitystream_Subject();
        
        $as_subject->addChildElement(Activitystream_ObjectType::buildFromDOMElement($elm->getElementsByTagName(Activitystream_ObjectType::ELEMENT_OBJECTTYPE)->item(0)));
        
        $as_subject->addChildElement(Atom_Feed_Id::buildFromDOMElement($elm->getElementsByTagName(Atom_Feed_Id::ELEMENT_ID)->item(0)));
        $as_subject->addChildElement(Atom_Feed_Title::buildFromDOMElement($elm->getElementsByTagName(Atom_Feed_Title::ELEMENT_TITLE)->item(0)));
        
        foreach ($elm->getElementsByTagName(Atom_Feed_Link::ELEMENT_LINK) as $elmLink) {
            
            if( $elmLink->parentNode->nodeName !== 'activity:subject') {
                continue;
            }
            
            if($elmLink->getAttribute(Atom_Feed_Link_Avatar::ATTRIBUTE_REL) == Atom_Feed_Link_Avatar::REL_VALUE) {
                $as_subject->addChildElement(Atom_Feed_Link_Avatar::buildFromDOMElement($elmLink));
            } else if($elmLink->getAttribute(Atom_Feed_Link_AlternateHtml::ATTRIBUTE_REL) == Atom_Feed_Link_AlternateHtml::REL_VALUE) {
                $as_subject->addChildElement(Atom_Feed_Link_AlternateHtml::buildFromDOMElement($elmLink));
            }
            
        }

        return $as_subject; 
        
    }
}